﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PSSMS.View.AccPay
{
    /// <summary>
    /// Provider_xg.xaml 的交互逻辑
    /// </summary>
    public partial class Provider_xg : Window
    {
        /// <summary>
        /// 声明全局变量
        /// </summary>
        DataRowView DGVR;
        bool blinsert = false;//默认操作
        BLL.Providersm.ProvidersmClient myClient = new BLL.Providersm.ProvidersmClient();
        public Provider_xg(DataRowView data)
        {
            InitializeComponent();
            DGVR = data; //接收传递的选中行
        }
       

        /// <summary>
        /// 窗口加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 1、下拉框绑定
            //供应商类型
            DataTable dtSex = myClient.SelectSrType().Tables[0];
            ghslx.DisplayMemberPath = "supplierType";
            ghslx.SelectedValuePath = "supplierTypeID";
            ghslx.ItemsSource = dtSex.DefaultView;
            //所属地区
            DataTable dtWorkStatus = myClient.SelDisName().Tables[0];
            ssdq.DisplayMemberPath = "disName";
            ssdq.SelectedValuePath = "disID";
            ssdq.ItemsSource = dtWorkStatus.DefaultView;
            ////结算方式
            DataTable dtPost = myClient.SelectPmethod().Tables[0];
            jsfs.DisplayMemberPath = "paymentMethod";
            jsfs.SelectedValuePath = "paymentMethodID";
            jsfs.ItemsSource = dtPost.DefaultView;
            #endregion

            #region 2、回填数据
            ghsmc.Text = DGVR.Row["supplierName"].ToString().Trim();
            ghslx.SelectedValue = Convert.ToInt32(DGVR.Row["supplierTypeID"]);
            ssdq.SelectedValue = Convert.ToInt32(DGVR.Row["disID"]);
            lxr.Text = DGVR.Row["contactPerson"].ToString();
            lxdh.Text = DGVR.Row["contactNumber"].ToString();
            qcfk.Text = DGVR.Row["initialPayable"].ToString();
            jsfs.SelectedValue = Convert.ToInt32(DGVR.Row["paymentMethodID"]);
            //把字符串类型转换成布尔值类型
            if (DGVR.Row["defaultNo"].ToString() == "默认")
            {
                mrgys.IsChecked = true;
            }
            if (DGVR.Row["disabledNo"].ToString() == "禁用")
            {
                jy.IsChecked = true;
            }
            lxdz.Text = DGVR.Row["contactAddress"].ToString();
            bz.Text = DGVR.Row["remark"].ToString();
            #endregion
        }

        /// <summary>
        /// 验证期初付款价格只能输入数值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void jsfs_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(qcfk.Text))
                {
                    //浮点数正则表达式
                    if (!Regex.IsMatch(qcfk.Text.Trim(), @"^[1-9]\d*|^[1-9]\d*.\d+[1-9]$"))
                    {
                        MessageBox.Show("期初付款价格只能是金额数值！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                        qcfk.Text = string.Empty;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 修改保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断必填项不能为空
                if (ghslx.SelectedValue != null && ssdq.SelectedValue != null && jsfs.SelectedValue != null)
                {
                    //2、获取控件值                   
                    string strSupplierName = ghsmc.Text.ToString();//供货商名称
                    int intSupplierTypeID = Convert.ToInt32(ghslx.SelectedValue);//供货商类型
                    int intDisID = Convert.ToInt32(ssdq.SelectedValue);//所属地区
                    string strContactPerson = lxr.Text.ToString();//联系人
                    string strContactNumber = lxdh.Text.ToString();//联系电话
                    decimal decInitialPayable = Convert.ToDecimal(qcfk.Text);//期初付款
                    int intPaymentMethodID = Convert.ToInt32(jsfs.SelectedValue);//结算方式
                    bool blDefaultNo = (bool)mrgys.IsChecked;//默认供应商
                    bool blDisabledNo = (bool)jy.IsChecked;//禁用
                    string strContactAddress = lxdz.Text.ToString();//联系地址
                    string strRemark = bz.Text.ToString();//备注
                    int intSupplierID = Convert.ToInt32(DGVR.Row["supplierID"]);
                    //3、代用服务方法执行修改
                    int intCount = myClient.UpdateStaff(strSupplierName, strContactPerson,
             strContactNumber, intSupplierTypeID, strRemark, intDisID, decInitialPayable,
             intPaymentMethodID, strContactAddress, blDefaultNo,  blDisabledNo, intSupplierID);
                    //4、根据返回结果做提示
                    if (intCount > 0)
                    {
                        MessageBoxResult dr = MessageBox.Show("修改【" + strSupplierName + "】供货商信息！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
                        this.Close();
                    }
                    else if (intCount == -1)
                    {
                        MessageBox.Show("修改供货商信息重复！", "系统提示：", MessageBoxButton.OK, MessageBoxImage.Stop);
                    }
                    else
                    {
                        MessageBoxResult dr = MessageBox.Show("修改【" + decInitialPayable + "】供货商信息有误！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);//弹出确定对话框
                    }
                }
                else
                {
                    MessageBox.Show("请检查是否存在空数据。", "系统提示：", MessageBoxButton.OK, MessageBoxImage.Stop);
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "系统提示：", MessageBoxButton.YesNo, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("关闭窗口，无法保存数据，是否要关闭？", "系统提示：", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                //关闭窗口
                this.Close();
            }
        }
 
    }
}
